package com.woniu.tms.login.service.impl;

import com.woniu.tms.entity.*;
import com.woniu.tms.login.MD5Utils;
import com.woniu.tms.login.service.LoginService;
import com.woniu.tms.manage.utils.MyUtils;
import com.woniu.tms.mapper.RoleMapper;
import com.woniu.tms.mapper.UserMapper;
import com.woniu.tms.mapper.UserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: peng
 */
@Service
@Transactional
public class LoginServiceImpl implements LoginService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Autowired
    RoleMapper roleMapper;

    /**
     * 登录方法：账号密码正确返回，实体user，错误返回null
     *
     * @param user
     * @return
     */
    @Override
    public User login(User user) {
        Boolean flag = true;

        if (flag) {
            UserExample userExample = new UserExample();
            UserExample.Criteria userExampleCriteria = userExample.createCriteria();
            userExampleCriteria.andSnoEqualTo(user.getSno());
            if (!userMapper.selectByExample(userExample).isEmpty()) {
                String saltPassword = userMapper.selectByExample(userExample).get(0).getUpwd();
                if (MD5Utils.verify(user.getUpwd(), saltPassword)) {
                    user = userMapper.selectByExample(userExample).get(0);
                } else {
                    throw new RuntimeException("用户名正确，密码错误");
                }

            } else {
                UserExample userExample1 = new UserExample();
                UserExample.Criteria criteria = userExample1.createCriteria();
                criteria.andUphoneEqualTo(user.getUphone());
                if (!userMapper.selectByExample(userExample1).isEmpty()) {
                    String saltPassword = userMapper.selectByExample(userExample1).get(0).getUpwd();
                    if (MD5Utils.verify(user.getUpwd(), saltPassword)) {
                        user = userMapper.selectByExample(userExample1).get(0);
                    } else {
                        throw new RuntimeException("用户名正确，密码错误");
                    }
                }
            }
            return user;
        } else {
            throw new RuntimeException("登录失败，请检查密码或账户名是否正确");
        }
    }

    /**
     * 检验密码是否正确
     *
     * @param password
     * @param saltPassword
     * @return
     */
    private boolean verify(String password, String saltPassword) {
        return MD5Utils.verify(password, saltPassword);
    }


    /**
     * 登录时获取用户的角色
     *
     * @param uid
     * @return
     */
    @Override
    public List<Role> getRole(Integer uid) {
        UserRoleExample example = new UserRoleExample();
        UserRoleExample.Criteria criteria = example.createCriteria();
        criteria.andUidEqualTo(uid);
        List<UserRole> userRoleList = userRoleMapper.selectByExample(example);
        List<Integer> rids = new ArrayList<>();
        for (UserRole ur : userRoleList) {
            rids.add(ur.getRid());
        }
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria roleExampleCriteria = roleExample.createCriteria();
        if (rids.size() > 0) {
            roleExampleCriteria.andRidIn(rids);
        }
        List<Role> roles = roleMapper.selectByExample(roleExample);

        return roles;
    }

    @Override
    public List<Permission> getTree(User user) {
        List<Permission> permissionList = userMapper.getPermissions(user.getUid());
        return MyUtils.getTree(permissionList);
    }

    @Override
    public Set<String> getUserPermissions(User user) {
        List<Permission> permissions = userMapper.getPermissions(user.getUid());
        Set<String> set = new HashSet<>();
        for (Permission permission : permissions) {
            if (permission.getUrl() != null && !"".equals(permission.getUrl())) {
                set.add(permission.getUrl());
            }
        }
        return set;
    }
}
